// components/phaq/phaq.js
import dayjs from 'dayjs'
Component({

  /**
   * Component properties
   */
  properties: {
    items: {
      type: Array,
      value: []
    }
  },

  /**
   * Component initial data
   */
  data: {
    formattedItems:[]
  },

  observers: {
    'items': function (items) {
      console.log(items,"hiiiii");
      const f = items.map(item => ({
        ...item,
        formattedTime: this.formatRelativeDate(item.createTime)
      }));
      console.log(f,"helloworld");
      this.setData({ formattedItems:f });
    }
  },

  /**
   * Component methods
   */
  methods: {
    formatRelativeDate(date) {
      const now = dayjs();
      const targetDate = dayjs(date);
      const diffDays = now.diff(targetDate, 'day');

      if (diffDays === 0) {
        return '今天';
      } else if (diffDays === 1) {
        return '昨天';
      } else if (diffDays === 2) {
        return '前天';
      } else if (diffDays > 2 && diffDays <= 7) {
        // return targetDate.fromNow();  // 会返回类似 "3天前" 的字符串
        return `${diffDays}天前`;
      } else {
        return targetDate.format('YYYY-MM-DD');  // 超过一周直接显示日期
      }
    },

    transferId(el){
      console.log(el);
      this.triggerEvent("getID",{id:el.target.dataset.id})
    }
  }
})